package com.imoresoft.magic.util;

import com.ambitor.grass.sql.dao.SuperDao;
import com.ambitor.grass.util.data.IData;
import com.ambitor.grass.util.data.IDataset;
import com.imoresoft.magic.property.AppConstants;

public class AreaUtil {

	public static void main(String[] args) {

	}

	public static String getCityId(SuperDao superDao, String areaInfo)
			throws Exception {
		IData areaData = getAreaData(superDao, areaInfo);
		if (areaData == null) // 找不到，返回杭州
			return "101210101";

		String isMuni = areaData.getString("IS_MUNI");// 是否直辖市
		String citysStr = areaData.getString("CITYS_STR");
		if (AppConstants.FLAG_YES.equals(isMuni)) {
			return "101" + citysStr.split("|")[0];
		}
		String[] cityArray = citysStr.split(",");
		String findCityStr = null;
		for (String cityStr : cityArray) {
			if (areaInfo.contains(cityStr)) {
				findCityStr = cityStr;
				break;
			}
		}

		if (findCityStr == null) {
			return "101" + areaData.getString("DEFAULT_CITY");
		} else {
			return "101" + findCityStr.split("|")[0];
		}
	}

	public static IData getAreaData(SuperDao superDao, String areaInfo)
			throws Exception {
		IDataset dataset = superDao.queryList("TD_M_PROV");
		int findIndex = -1;
		for (int index = 0; index < dataset.size(); index++) {
			String provName = (String) dataset.get(index, "PROV_NAME");
			if (areaInfo.contains(provName)) {
				findIndex = index;
				break;
			}
		}

		if (findIndex == -1)
			return null;

		IData areaData = dataset.getData(findIndex);
		return areaData;
	}

}
